# -*- coding: utf-8 -*-


from PyQt4 import QtCore, QtGui
import sys, thread, os, time
#from fonk import VERSION, byte
#from noplatform import *
import arac


class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(400, 150)
        self.setMinimumSize(400,150)
        self.setMaximumSize(400,150)
        self.setWindowTitle(u"ODUN - Updater")
        #self.setWindowIcon(QtGui.QIcon("ikon.png"))
        self.cw = QtGui.QWidget(self)
        self.setCentralWidget(self.cw)
        self.mbar = QtGui.QMenuBar(self)
        self.mDosya = QtGui.QMenu(self.mbar)
        self.mYardim = QtGui.QMenu(self.mbar)
        self.setMenuBar(self.mbar)
        self.sbar = QtGui.QStatusBar(self)
        self.setStatusBar(self.sbar)
        self.aHakkinda = QtGui.QAction(self)
        self.aKullanimKlavuzu = QtGui.QAction(self)
        self.aQtHakkinda = QtGui.QAction(self)
        self.aQtHakkinda.setText(u"Qt Hakk�nda")
        self.aKapat = QtGui.QAction(self)
        self.aKapat.setText("Kapat")
        self.mDosya.addAction(self.aKapat)
        self.mYardim.addAction(self.aKullanimKlavuzu)
        self.mYardim.addSeparator()
        self.mYardim.addAction(self.aHakkinda)
        self.mYardim.addAction(self.aQtHakkinda)
        self.mbar.addAction(self.mDosya.menuAction())
        self.mbar.addAction(self.mYardim.menuAction())
        self.mDosya.setTitle(u"Dosya")
        self.mYardim.setTitle(u"Yard�m")
        self.aHakkinda.setText(u"ODUN Hakk�nda")
        self.aKullanimKlavuzu.setText(u"Kullan�m Klavuzu")

        QtCore.QObject.connect(self.aKapat, QtCore.SIGNAL("triggered(bool)"), self.close)
        QtCore.QObject.connect(self.aHakkinda, QtCore.SIGNAL("triggered(bool)"), self.hakkinda)
        QtCore.QObject.connect(self.aKullanimKlavuzu, QtCore.SIGNAL("triggered(bool)"), self.klavuz)
        QtCore.QObject.connect(self.aQtHakkinda, QtCore.SIGNAL("triggered(bool)"), QtGui.QApplication.aboutQt)       

        self.gLayout = QtGui.QGridLayout(self.cw)
        self.label = QtGui.QLabel(self.cw)
        self.label2 = QtGui.QLabel(self.cw)
        self.gLayout.addWidget(self.label2, 0,2,1,1)
        self.label2.hide()
        self.gLayout.addWidget(self.label, 0,2,1,1)
        self.cHayir = QtGui.QRadioButton(self.cw)
        self.cHayir.setChecked(True)
        self.gLayout.addWidget(self.cHayir, 2,0,1,1)
        self.cEvet = QtGui.QRadioButton(self.cw)
        self.gLayout.addWidget(self.cEvet, 1,0,1,1)
        self.kaydet = arac.QButton(self.cw)
        self.gLayout.addWidget(self.kaydet, 1,2,1,1)
        self.soru1 = QtGui.QLabel(self.cw)
        self.gLayout.addWidget(self.soru1, 0,0,1,1)
        self.updated = arac.QButton(self.cw)
        self.updated.hide()
        self.gLayout.addWidget(self.updated, 1,2,1,1)
        self.moviel = QtGui.QLabel(self.cw)
        self.moviel2 = QtGui.QLabel(self.cw)
        self.loading = QtGui.QMovie("loading.gif")
        self.loading2 = QtGui.QMovie("loading.gif")
        self.moviel.setMovie(self.loading)
        self.moviel2.setMovie(self.loading2)
        self.gLayout.addWidget(self.moviel, 1,3,1,1)
        self.gLayout.addWidget(self.moviel2, 1,3,1,1)
            
        self.label.setText(u"Mevcut Paketleri Al:")
        self.label2.setText(u"G�ncellemeleri Al:")
        self.cHayir.setText(u"Hay�r")
        self.cEvet.setText(u"Evet")
        self.kaydet.setText(u"Paketleri Al")
        self.soru1.setText(u"�nternetin var m�?")
        self.updated.setText(u"Al")
           
        #QtCore.QObject.connect(self.kaydet, QtCore.SIGNAL("clicked(bool)"), self._xmlKaydet)
        QtCore.QObject.connect(self.cHayir, QtCore.SIGNAL("clicked(bool)"), self.checkbox)
        QtCore.QObject.connect(self.cEvet, QtCore.SIGNAL("clicked(bool)"), self.checkbox)
        #QtCore.QObject.connect(self.updated, QtCore.SIGNAL("clicked(bool)"), self._getUpdatedList)

    def _xmlKaydet(self):
        thread.start_new_thread(self.xmlKaydet, ())

    def xmlKaydet(self):
        self.kaydet.setEnabled(False)
        self.loading2.start()
        print "Y�kl� paketler al�n�yor..."
        #pkg.getInstalledPackages()
        print "Y�kl� paketler al�nd�."
        time.sleep(2)
        self.loading2.stop()
        repo = open("/tmp/installed-package.xml","r")
        xmlyaz = open(os.environ["HOME"]+"/installed-package.xml","w")
        xmlyaz.write(repo.read())
        xmlyaz.close()
        print "Kullan�c� dizininde 'installed-package.xml' dosyas� olu�turuldu."
        self.kaydet.setEnabled(True)


    def _getUpdatedList(self):
        thread.start_new_thread(self.getUpdatedList, ())

    def getUpdatedList(self):
        self.updated.setEnabled(False)
        self.loading.start()
        print "G�ncellemeler al�n�yor..."
        #paket = Package()
        #paket.writeUpdatedPackages()
        print "G�ncellemeler al�nd�."
        time.sleep(2)
        self.loading.stop()
        repo = open("/tmp/updates.pdx","r")
        xmlyaz = open(os.environ["HOME"]+"/updates.pdx","w")
        xmlyaz.write(repo.read())
        xmlyaz.close()
        print "Kullan�c� dizininde 'updates.pdx' dosyas� olu�turuldu."
        self.updated.setEnabled(True)
        
    
        
    def checkbox(self):
        if self.cEvet.isChecked():
            self.label.hide()
            self.moviel2.hide()
            self.kaydet.hide()
            self.updated.show()
            self.moviel.show()
            self.label2.show()
            
        if self.cHayir.isChecked():
            self.updated.hide()
            self.moviel.hide()
            self.label2.hide()
            self.label.show()
            self.moviel2.show()
            self.kaydet.show()
             
    def hakkinda(self):
        mesaj = u"""<html>
        Lisans: GPL v3<br>
        S�r�m: %s<br>
        <b>ODUN</b> � 2010<br>
        <b>Kodlayanlar:</b><br>
        A�k�n Yollu   - <a href="mailto:askin@askin.ws">askin@askin.ws</a><br>
        Erdem Artan   - <a href="mailto:badibere@gmail.com">badibere@gmail.com</a><br>
        Metehan �zbek - <a href="mailto:metehan@istanbul.com">metehan@istanbul.com</a><br>
        <br>Proje Sayfas�:
        <a href='http://code.google.com/p/pisidown'>http://code.google.com/p/pisidown</a>
        </html>
        """#%VERSION
        mesajk = QtGui.QMessageBox.about(self.cw, u"ODUN Hakk�nda", mesaj)
    
    def klavuz(self):
        pass
        

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    sys.exit(app.exec_())
    
